package com.zhuo.delivery.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhuo.delivery.common.BaseContext;
import com.zhuo.delivery.common.R;
import com.zhuo.delivery.entity.Orders;
import com.zhuo.delivery.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("接收到的订单：{}",orders);
        orderService.submit(orders);
        return R.success("下单成功");
    }

    /**
     * 订单展示分页功能
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page> userPage(Integer page,Integer pageSize){
        log.info("page:{},pageSize:{}",page,pageSize);
        Page<Orders> pageInfo = new Page<>(page,pageSize);
        Long userId = BaseContext.getCurrentId();
        //查询订单
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getUserId,userId);
        orderService.page(pageInfo,wrapper);
        return R.success(pageInfo);
    }
}
